﻿using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace TurtleEngine
{
	 /// <summary>
	 ///
	 /// </summary>
	 internal class HTMLTransfomer
	 {
		  /// <summary>
		  /// The _HTML file
		  /// </summary>
		  private readonly string _htmlFile = string.Empty;

		  /// <summary>
		  /// The _xmlfile
		  /// </summary>
		  private readonly string _xmlfile = string.Empty;

		  /// <summary>
		  /// The _XSL file
		  /// </summary>
		  private readonly string _xslFile = string.Empty;

		  /// <summary>
		  /// Initializes a new instance of the <see cref="HTMLTransfomer" /> class.
		  /// </summary>
		  /// <param name="xmlfile">The xmlfile.</param>
		  /// <param name="xslFile">The XSL file.</param>
		  /// <param name="htmlFile">The HTML file.</param>
		  public HTMLTransfomer(string xmlfile, string xslFile, string htmlFile)
		  {
				_xmlfile = xmlfile;
				_xslFile = xslFile;
				_htmlFile = htmlFile;
		  }

		  /// <summary>
		  /// Tables the transformer.
		  /// </summary>
		  public void TableTransformer()
		  {
				var xmlSource = new XmlTextReader(@"xml\" + _xmlfile);
				var xpathDoc = new XPathDocument(xmlSource);

				var xslSource = new XmlTextReader(@"xsl\" + _xslFile);
				var xsltDoc = new XslCompiledTransform();

				xsltDoc.Load(xslSource);

				var sb = new StringBuilder();
				var sw = new StringWriter(sb);

				var ar = new XsltArgumentList();

				xsltDoc.Transform(xpathDoc, null, sw);

				File.WriteAllText(@"html\" + _htmlFile, sb.ToString());
		  }
	 }
}